//
//  PlayModel.m
//  GC_OC
//
//  Created by 曾霄逸 on 2022/3/6.
//

#import "PlayModel.h"
#import "PlayViewController.h"
#import "ICMusicPlayManager.h"
#import "ObjectAFNetWorking.h"
#import "GC_OCTool.h"
#import "ICMusicPlayModel.h"
#import "ICMusicPlayNarrowView.h"
#import "PlayeMediaViewController.h"
#import "HSVideoPlayViewController.h"
#define NarrowViewH     64
#define NarrowViewW     228
@interface PlayModel() {
    PlayStyle _playStyle;
}
//标题
@property (nonatomic, strong, readwrite) NSString * titleStr;

/**播放列表*/
@property (nonatomic, strong, readwrite) NSMutableArray <CurriculumModel *> * listArr;

@property (nonatomic, assign, readwrite) NSInteger currentIndex;

@property (nonatomic, strong, readwrite) CurriculumModel *currentModel;
@property (nonatomic, strong) ObjectAFNetWorking* objectAFN;
@property (nonatomic, strong) ICMusicPlayModel *playAutoModel;
@property (nonatomic, strong) ICMusicPlayNarrowView *narrowView;
@end

@implementation PlayModel
+(instancetype)sharedModel
{
    static PlayModel* model=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        model = [[super allocWithZone:NULL] init];
        
    });
    return model;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.listArr = [NSMutableArray array];
        self.currentIndex = 0;
        _objectAFN = [[ObjectAFNetWorking alloc]init];
    }
    return self;
}

- (void)play:(CurriculumModel *)model listArr:(NSArray<CurriculumModel *> *)listArr viewPlayY:(CGFloat)playY {
    [self.listArr removeAllObjects];
    [self.listArr addObjectsFromArray:listArr];
    for (NSInteger index = 0; index < listArr.count; index ++) {
        CurriculumModel *newModel = listArr[index];
        if ([newModel.id isEqualToString:model.id]) {
            [[PlayModel sharedModel] changeIndex:index];
//            [self getDataWithID:newModel.id];
            if (newModel.audio_num.floatValue > 0) {
                ICMusicPlayManager *musicManager = [ICMusicPlayManager sharedManager];
                //准备资源
                [musicManager loadMusicSouceWithMusicArray:[NSMutableArray arrayWithArray:listArr] Options:(ICMusicModePlayerOptions){.isNeedCyclePlay = NO,.isShowBackPlayInfo = YES,.playViewY = playY,.playIndex = (int)self.currentIndex} Complete:^(ICMusicPlayModel * _Nullable model) {
                    
                    //开始播放
                    [musicManager beginPlayUpDataViewWithModel:model NarrowViewStatue:self->_narrowView.showState];
                }];

            }else{
                [self getDataWithID:model.id];
            }
                      
            break;
        }
    }
   
}

-(void)getDataWithID:(NSString *)idStr
{
    [FKCacheTool getMediaDetail:idStr complete:^(id data) {
        self.playAutoModel = [ICMusicPlayModel mj_objectWithKeyValues:data];
        HSVideoPlayViewController *vc = [HSVideoPlayViewController new];
        vc.currentModel = self.playAutoModel;
        [[UIViewController currentViewController].navigationController pushViewController:vc animated:YES];
    }];
}

- (void)changeIndex:(NSInteger)index {
    self.currentIndex = index;
}

- (void)changePlayStyle {
    NSInteger style = self.playStyle;
    style += 1;
    self.playStyle = style % 3;
}

- (void)playLastSong {
    switch (_playStyle) {
        case PlayStyleSequence: {
            //顺序播放情况
            if (self.currentIndex == 0) {
                self.currentIndex = self.listArr.count - 1;
            }else {
                self.currentIndex -= 1;
            }
        }
            break;
        case PlayStyleSingle :
            break;
        case PlayStyleRandom:
            self.currentIndex = arc4random() % self.listArr.count;
        default:
            break;
    }
    
}

- (void)playNextSong {
    switch (_playStyle) {
        case PlayStyleSequence: {
            //顺序播放情况
            if (self.currentIndex == self.listArr.count - 1) {
                self.currentIndex = 0;
            }else {
                self.currentIndex += 1;
            }
        }
            break;
        case PlayStyleSingle :
            break;
        case PlayStyleRandom:
            self.currentIndex = arc4random() % self.listArr.count;
        default:
            break;
    }
}

- (CurriculumModel *)currentModel {
    if (self.listArr.count == 0) {
        return nil;
    }
    CurriculumModel *model = self.listArr[self.currentIndex];
    [[LearningHistoryService sharedInstance] saveHistoryInfo:[model dictionary]];
    return model;
}


- (void)setPlayStyle:(PlayStyle)playStyle {
    _playStyle = playStyle;
    [[NSUserDefaults standardUserDefaults] setInteger:playStyle forKey:@"playStyle"];
}

- (PlayStyle)playStyle {
    NSInteger playStyle = [[NSUserDefaults standardUserDefaults]integerForKey:@"playStyle"];
    if (playStyle < 0) {
        playStyle = PlayStyleSequence;
    }
    _playStyle = playStyle;
    return (PlayStyle)playStyle;
}
@end
